﻿using System;
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.XWPF.UserModel;

namespace MyHelpers
{
    public static class DocxHelper
    {
        #region 表格操作

        /// <summary>
        /// 创建行
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static XWPFTableRow CreateRow(XWPFTable table)
        {
            CT_Row ctRow = new CT_Row();
            XWPFTableRow row = new XWPFTableRow(ctRow, table);
            row.GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)426;
            table.AddRow(row);

            return row;
        }

        /// <summary>
        /// 创建列
        /// </summary>
        /// <param name="row"></param>
        /// <param name="width"></param>
        /// <returns></returns>
        public static XWPFTableCell CreateCell(XWPFTableRow row, int width = 2400)
        {
            XWPFTableCell cell = row.CreateCell();
            CT_Tc cttc = cell.GetCTTc();
            CT_TcPr ctpr = cttc.AddNewTcPr();

            cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;//水平居中

            ctpr.AddNewVAlign().val = ST_VerticalJc.center;//垂直居中

            ctpr.tcW = new CT_TblWidth
            {
                w = width.ToString(),
                type = ST_TblWidth.dxa
            };

            return cell;
        }

        /// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="span"></param>
        public static void SetColSpan(XWPFTableCell cell, int span)
        {
            CT_Tc cttc = cell.GetCTTc();
            CT_TcPr ctpr = cttc.AddNewTcPr();
            ctpr.gridSpan = new CT_DecimalNumber {val = span.ToString()};//合并单元格

            cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;//水平居中

            ctpr.AddNewVAlign().val = ST_VerticalJc.center;//垂直居中
        }

        #endregion
    }
}